<template>
	<view>
		<!-- 顶部搜索组件 -->
		<!-- 通过属性定制我组件显示的样式 -->
		<view class="search-box">
			<my-search bgcolor="#c0f000" radius="9" @click="gotoSearch"></my-search>
		</view>
		<!-- 分类选择 -->
		<view class="cate-container">
			<!-- 左侧一级导航 -->
			<scroll-view class="cate-left" scroll-y :style="{height: wh+'px'}">
				<view :class="['cate-left-item', i===active ? 'active':'']" v-for="(item, i) in cateList" :key="i"
					@click="changeActive(i)">
					{{item.cat_name}}
				</view>
			</scroll-view>

			<!-- 右侧二级导航 -->
			<scroll-view class="cate-right" :style="{height: wh+'px'}" scroll-y :scroll-top="scrollTop">
				<view class="cate-right-item" v-for="(item, i) in cateLevel2" :key="i">
					<!-- 二级分类标题 -->
					<view class="cate-right-item-title">
						/{{item.cat_name}}/
					</view>
					<!-- 二级分类具体内容 -->
					<view class="cate-right-item-content">
						<view class="cate-right-item-content-box" v-for="(item2,i2) in item.children" :key="i2"
							@click="gotoGoodsList(item2)">
							<image :src="item2.cat_icon" mode="widthFix"></image>
							<text>{{item2.cat_name}}</text>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import badgeMix from '@/mixins/setBadge.js'
	export default {
		mixins: [badgeMix],
		data() {
			return {
				// 定义数据容器
				cateList: [],
				// 二级分类数据
				cateLevel2: [],
				// 屏幕可用高度
				wh: 0,
				active: 0,
				// scroll-view 的bar举例顶部距离
				scrollTop: 100
			};
		},
		onLoad() {
			// 获取屏幕可用高度
			const sysInfo = uni.getSystemInfoSync()
			this.wh = sysInfo.windowHeight
			console.log(this.wh)
			// 发起请求
			this.getCateList()
		},

		methods: {
			// 跳转到搜索页
			gotoSearch() {
				console.log('父组件执行函数')
				uni.navigateTo({
					url: '/subpkg/search/search'
				})
			},
			// 点击商品进入商品列表页
			gotoGoodsList(goods) {
				uni.navigateTo({
					url: `/subpkg/goods_list/goods_list?cid=${goods.cat_id}`
				})
			},
			// 改变一级分类索引
			changeActive(i) {
				this.active = i
				console.log(i)
				// 改变二级分类数据
				this.cateLevel2 = this.cateList[i].children
				this.scrollTop = Math.random()
			},
			// 获取分类数据
			async getCateList() {
				const {
					data: res
				} = await uni.$http.get('/categories')
				// 数据异常判断
				if (res.meta.status !== 200) return uni.$showMessage()
				this.cateList = res.message
				// 二级分类·赋值
				this.cateLevel2 = res.message[0].children
			}
		}
	}
</script>

<style lang="scss">
	.search-box {
		// 设置定位效果为“吸顶”
		position: sticky;
		// 吸顶的“位置”
		top: 0;
		// 提高层级，防止被轮播图覆盖
		z-index: 9;
	}

	.cate-container {
		display: flex;

		.cate-left {
			width: 240rpx;

			.cate-left-item {
				display: flex;
				justify-content: center;
				align-items: center;
				height: 120rpx;
				background-color: #efefef;
				font-size: 24rpx;

				// 选中样式
				&.active {
					display: flex;
					justify-content: space-between;
					background-color: #ffffff;

					// 左边红线
					&::before {
						content: '';
						width: 6rpx;
						height: 60rpx;
						background-color: #c00000;
					}

					// 右边白线和背景相同
					&::after {
						content: '';
						width: 6rpx;
						height: 60rpx;
						background-color: #ffffff;
					}
				}
			}
		}

		.cate-right {
			.cate-right-item {
				.cate-right-item-title {
					display: flex;
					justify-content: center;
					font-weight: bold;
					font-size: 24rpx;
					padding: 20rpx 0;
				}

				.cate-right-item-content {
					display: flex;
					flex-wrap: wrap;

					.cate-right-item-content-box {
						width: 33.33%;
						display: flex;
						flex-direction: column;
						justify-content: center;
						align-items: center;
						width: 33.33%;
						padding: 20rpx;
						box-sizing: border-box;

						image {
							width: 100%;
						}

						text {
							font-size: 24rpx;
						}
					}
				}
			}
		}
	}
</style>
